package com.winz.cultureapp.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.LoginUserBean;
import com.winz.cultureapp.entity.UserChangPwdBean;
import com.winz.cultureapp.entity.UserInfoBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.ChangPwdReq;
import com.winz.cultureapp.http.request.UserInfoReq;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;
import com.winz.cultureapp.widge.SelEducationDialog;
import com.winz.cultureapp.widge.SelSexDialog;
import com.winz.cultureapp.widge.TimerSelector;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 个人信息中心
 */
public class UserInfoActivity extends BaseAppCompatActivity implements TimerSelector.OkClickListener {
    private static final int BIRTH_TIME = 0x000001;

    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.et_name)
    EditText mEt_name;
    @BindView(R.id.et_phone)
    EditText mEt_phone;
    @BindView(R.id.et_school)
    EditText mEt_school;
    @BindView(R.id.et_company)
    EditText mEt_company;
    @BindView(R.id.et_hobby)
    EditText mEt_hobby;
    @BindView(R.id.et_introduce)
    EditText mEt_introduce;
    @BindView(R.id.tv_sex)
    TextView mTv_sex;
    @BindView(R.id.tv_birth)
    TextView mTv_birth;
    @BindView(R.id.tv_edu)
    TextView mTv_edu;
    @BindView(R.id.tv_right)
    TextView mTv_right;
    @BindView(R.id.rl_right)
    RelativeLayout mRl_right;

    private SelSexDialog mSel_sex;
    private SelEducationDialog mSel_edu;

    private String sex; // 当前选择性别记录
    private String edu; // 当前选择学历记录
    private TimerSelector ts_birht;
    private String birthDate; // 当前选择的日期
    private long birth;


    @Override
    protected int contentView() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText(getString(R.string.mine_title1));
        mTv_right.setText("保存");
        mRl_right.setVisibility(View.VISIBLE);
        mTv_right.setVisibility(View.VISIBLE);

        mSel_sex = new SelSexDialog(this);
        mSel_edu = new SelEducationDialog(this);
        ts_birht = new TimerSelector(this, "yyyy年MM月dd日");
        ts_birht.setOkClickListener(this);
    }

    @Override
    protected void initEvents() {
        mSel_sex.setOkButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSel_sex.dismiss();
                sex = mSel_sex.getSelSex();
                mTv_sex.setText(sex);
            }
        });
        mSel_edu.setOkButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSel_edu.dismiss();
                edu = mSel_edu.getSelSex();
                mTv_edu.setText(edu);
            }
        });
    }

    @OnClick({R.id.fl_back, R.id.ll_sex, R.id.ll_birth, R.id.ll_edu, R.id.btn_submit,R.id.rl_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.ll_sex: // 选择性别
                mSel_sex.show();
                break;
            case R.id.ll_birth: // 选择出生
                ts_birht.show(mTv_birth, BIRTH_TIME);
                break;
            case R.id.ll_edu: // 选择学历
                mSel_edu.show();
                break;
            case R.id.rl_right:
                if (isAllright()){
                    updateInfo();
                }
                break;
        }
    }

    private boolean isAllright() {
        if (TextUtils.isEmpty(getEditTextStr(mEt_name))){
            showMsg("请输入真实姓名");
            return false;
        }
        if (TextUtils.isEmpty(mTv_sex.getText())){
            showMsg("请选择性别");
            return false;
        }
        if (TextUtils.isEmpty(mTv_birth.getText())){
            showMsg("请选择出生年月");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_school))){
            showMsg("请输入毕业院校");
            return false;
        }
        if (TextUtils.isEmpty(mTv_edu.getText())){
            showMsg("请选择学历");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_company))){
            showMsg("请输入工作单位");
            return false;
        }


        return true;
    }

    /**
     * 修改个人信息
     */
    private void updateInfo() {
        showProgressDialog();
        HttpUitls.onPostByToken("user_info_update", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                new UserInfoReq(birthDate,
                        getEditTextStr(mEt_company),
                        getEditTextStr(mEt_introduce),
                        mTv_edu.getText().toString(),
                        getEditTextStr(mEt_name),
                        getEditTextStr(mEt_school),
                        "未知".equals(mTv_sex.getText().toString())?0:"男".equals(mTv_sex.getText().toString())?1:2,
                        getEditTextStr(mEt_hobby)),
                new OnResponeListener<UserInfoBean>() {
            @Override
            public void onSuccess(UserInfoBean bean) {
                Logger.d("user_info_update==>>", JsonUtil.getJsonFromObj(bean));
                HttpUitls.isExpire(bean.code);
                if (bean.code == 0) {
                    showMsg("修改成功");
                    getList();
                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("user_info_update==>>", error);
                dismissProgressDialog();
            }
        });
    }

    /**
     * 时间选择器
     *
     * @param view
     * @param date
     * @param time
     * @param flag
     */
    @Override
    public void okClick(View view, String date, long time, int flag) {
        if (flag == BIRTH_TIME) {
            mTv_birth.setText(date);
            birthDate = date;
            birth = time;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getList();
    }

    private void getList() {
        showProgressDialog();
        HttpUitls.onGet("user_info", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),new OnResponeListener<UserInfoBean>() {
            @Override
            public void onSuccess(UserInfoBean bean) {
                Logger.d("user_info==>>", JsonUtil.getJsonFromObj(bean));

                HttpUitls.isExpire(bean.code);


                if (bean.code == 0) {
                    mEt_name.setText(bean.data.realname == null?"":bean.data.realname);
                    mEt_phone.setText(bean.data.phone == null?"":bean.data.phone);
                    mTv_birth.setText(bean.data.birthday == null?"":bean.data.birthday);
                    mTv_sex.setText(bean.data.sex==0?"未知":bean.data.sex ==1?"男":"女");
                    mEt_school.setText(bean.data.schoolName == null?"":bean.data.schoolName);
                    mTv_edu.setText(bean.data.education == null?"":bean.data.education);
                    mEt_company.setText(bean.data.company == null?"":bean.data.company);
                    mEt_hobby.setText(bean.data.speciality == null?"":bean.data.speciality);
                    mEt_introduce.setText(bean.data.description == null?"":bean.data.description);
                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("user_info==>>", error);
                dismissProgressDialog();
            }
        });
    }
}
